package com.ala4.oxcafe.boot.pnative;

import com.ala4.oxcafe.common.utils.collection.CollUtil;
import lombok.SneakyThrows;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeSerialization;

import java.util.List;

/**
 * lambda 表达式注入到graal中
 *
 * @author ztp
 * @date 2023/8/18 11:53
 */
public class LambdaRegistrationFeature implements Feature {

    @SneakyThrows
    @Override
    public void duringSetup(DuringSetupAccess access) {
        // 这里只支持一个**的匹配
        List<String> classes = MultiPackageScanner.scanClasses("com.ala4.**.service.impl", "com.ala4.oxcafe.repository");
        // 忽略的类 spring自动生成的 不需要加载
        List<String> ignoreClass = CollUtil.newArrayList("__BeanDefinitions", "__Autowiring", "SpringCGLIB");
        for (String aClass : classes) {
            if (ignoreClass.stream().anyMatch(aClass::contains)) {
                continue;
            }
            Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(aClass);
            RuntimeSerialization.registerLambdaCapturingClass(clazz);
            // 需要确保类资源被扫描进去了
            System.out.println("成功加载类: " + aClass);
        }
    }

}
